package com.coollang.smartbra.activity;

import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.coollang.smartbra.MyAutoLayoutActivity;
import com.coollang.smartbra.R;
import com.coollang.smartbra.dialog.SportAnalyzeViewDialog;
import com.coollang.smartbra.interfaces.ActionType;
import com.coollang.smartbra.model.GoldAndActionModel;
import com.coollang.smartbra.presenter.SportAnalyzeActivityPresenter;
import com.coollang.smartbra.utils.ActionTypeParse;
import com.coollang.smartbra.utils.ActivitySwitcher;
import com.coollang.smartbra.utils.TimeUtils;
import com.coollang.smartbra.utils.TranslucentSystemBarUtils;
import com.coollang.smartbra.views.RadarData;
import com.coollang.smartbra.views.RadarView;
import com.coollang.smartbra.views.ScoreTrend;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.coollang.smartbra.R.id.radarView;

/**
 * 综合分析页面
 */

public class SportAnalyzeActivity extends MyAutoLayoutActivity {

    @BindView(R.id.status_bar)
    View statusBar;
    @BindView(R.id.content)
    LinearLayout content;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_add_day)
    TextView mTvAddDay;
    @BindView(R.id.tv_gold_count)
    TextView mTvGoldCount;
    @BindView(R.id.tv_action_time)
    TextView mTvActionTime;
    @BindView(R.id.scoreTrend)
    public ScoreTrend mScoreTrend;
    @BindView(radarView)
    public RadarView mRadarView;
    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.assess_time)
    TextView mAssessTime;
    @BindView(R.id.tv_score)
    TextView mTvScore;
    @BindView(R.id.tv_right_title)
    TextView mTvRightTitle;

    private SportAnalyzeActivityPresenter mPresenter;
    private static Integer[] ACTION_CODE = {
            ActionType.SDT, ActionType.YWQZ, ActionType.JF, ActionType.FWC, ActionType.PBZC,
            ActionType.TQ, ActionType.ZQ, ActionType.LTQ, ActionType.XFY, ActionType.ZZST, ActionType.JFZYMJ
    };

    @Override
    protected void initView() {
        setContentView(R.layout.activity_sport_analyze);
        ButterKnife.bind(this);

        // 设置沉浸式状态栏
        TranslucentSystemBarUtils.showChenJinZhuangTaiLan(statusBar, this, content);
        initViews();
    }

    /**
     * 初始化View
     */
    private void initViews() {
        tvTitle.setText("运动统计");
        tvTitle.setTextSize(16);
        tvTitle.setTextColor(ContextCompat.getColor(this, R.color.black_80));

        mTvRightTitle.setVisibility(View.VISIBLE);
        mTvRightTitle.setText("评估规则");
        mTvRightTitle.setTextSize(16);
        mTvRightTitle.setTextColor(getResources().getColor(R.color.progrss_bg));

        mIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finishActivity();
            }
        });

        int currentMonth = TimeUtils.getCurrentMonth() + 1;
        String currentMonthStr;
        if (currentMonth < 10) {
            currentMonthStr = "0" + currentMonth;
        } else {
            currentMonthStr = currentMonth + "";
        }

        mAssessTime.setText("评估时间 " + TimeUtils.getCurrentYear() + "-" + currentMonthStr + "-01");
    }

    public void setScoreText(String text) {
        SpannableString msp = new SpannableString(text);
        // 设置字体颜色
        msp.setSpan(new ForegroundColorSpan(0xFFF79444), 3, text.length() - 14, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        msp.setSpan(new ForegroundColorSpan(0xFFF79444), text.length() - 9, text.length() - 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        mTvScore.setText(msp);
    }

    @Override
    protected void initData() {
        setUI();

        mPresenter = new SportAnalyzeActivityPresenter(this);
        mPresenter.getActionsAmoun();
        mPresenter.getMonthBill();
        mPresenter.getFooterCount();

        GoldAndActionModel model = mPresenter.queryGoldAndAction();
        mTvAddDay.setText(model.getDay() + "");
        mTvGoldCount.setText(model.getCoin() + "");
        mTvActionTime.setText(model.getTime() / 60 + "");
    }

    private void setUI() {
        /**
        // 雷达图
        List<String> vertexText = new ArrayList<>();
        Collections.addAll(vertexText, "仰卧起坐", "俯卧撑", "卷腹", "跑步", "计步", "深蹲", "深蹲跳", "平板支撑");
        mRadarView.setVertexText(vertexText);
        mRadarView.setEmptyHint("");

        // 设置层数
        //  mRadarView.setLayer(1);

        // 设置点击事件
        mRadarView.setOnIndexClick(new OnIndexClick() {
            @Override
            public void onClick(int index) {
                ActivitySwitcher.goToSportStatistics1(SportAnalyzeActivity.this, index);
            }
        });
         */

        int length = ACTION_CODE.length;

        List<RadarData> dataList = new ArrayList<>();
        for (int i = 0; i < length; i++) {
            String actionName = ActionTypeParse.getActionName(ACTION_CODE[i]);
            RadarData data = new RadarData(actionName, 0);
            dataList.add(data);
        }
        mRadarView.setDataList(dataList);
        mRadarView.setOnIndexClick(new RadarView.OnIndexClick() {
            @Override
            public void onClick(int index) {
                ActivitySwitcher.goToSportStatistics1(SportAnalyzeActivity.this, index);
            }
        });
    }

    @OnClick(R.id.tv_right_title)
    public void onViewClicked() {
        // 弹出 dialog 显示页面
        SportAnalyzeViewDialog viewDialog = new SportAnalyzeViewDialog();

        // 获取当前运动等级
        viewDialog.show(getSupportFragmentManager(), "SportAnalyzeViewDialog");

        viewDialog.setOnSetlevel(new SportAnalyzeViewDialog.OnSetLevel() {
            @Override
            public int level() {
                return mStrength - 1;
            }
        });
    }

    private int mStrength = 1;

    public void setScoreText(int coin, int strength) {
        mStrength = strength;
        String levelStr = mPresenter.parserFooterData(strength);

        String text = "评分为 " + coin + " , 在 " + levelStr + " 的队伍中";
        setScoreText(text);
    }

    public static Integer[] getActionCode() {
        return ACTION_CODE;
    }
}
